<?php
$parentCaption = 'Merchants';
$caption = $model->name;
$this->pageTitle = $caption;

$this->breadcrumbs = array(
	$parentCaption => array('index'),
	$caption,
);

//define array
$aWeekDays = BUtils::WeekDayKeys(7, true);
$a24Hours = BUtils::AllHour24();
$hours_list = MerchantObject::HtmlBusinessHours($aHours, $aWeekDays, $a24Hours);

//get url_logo
$url_logo = BUtils::GetPictureUrl($model->url_logo);

//publish button
$btn_publish = '';
if ($model->status != 1) {
	$btn_publish = '<a class="btn btn-mini btn-info ajax-action" href="'
		. Yii::app()->createUrl('/tools/merchant/publish', array('id'=>$model->id))
		. '"><i class="icon-rss ajax-ready"></i><i class="icon-refresh icon-spin ajax-loading" style="display:none;"></i> Publish</a>';
}
?>
<div class="page-header position-relative">
	<h1>
		<?php echo $parentCaption; ?>
		<small>
			<i class="icon-double-angle-right"></i>
			<?php echo $caption; ?>
		</small>
	</h1>
	<div style="position:absolute; top:0; right:12px;">
		<a class="btn btn-info" href="<?php echo Yii::app()->createUrl('/tools/offer/form', array('mid'=>$model->id)); ?>">
			<i class="icon-certificate"></i> Create an offer
		</a>
		<a class="btn btn-blue" href="<?php echo Yii::app()->createUrl('/tools/offer', array('q'=>$model->id)); ?>">
			<i class="icon-search"></i> List <?php echo $offers; ?> offer(s)
		</a>
	</div>
	<div class="clearfix"></div>
</div><!--/.page-header-->

<div class="row-fluid">
	<div class="space-6"></div>
	
	<div class="row-fluid">
		<div class="span7">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter blue"><?php echo $model->name; ?></h3>
					<div class="widget-toolbar">
						<a href="<?php echo Yii::app()->createUrl('/tools/merchant/form', array('id'=>$model->id)); ?>">
							<i class="icon-edit"></i> Edit profile
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
					<div class="widget-main">
						<div class="business-profile-info">
							<div class="business-logo pull-right">
								<img src="<?php echo $url_logo; ?>" alt="" class="img-polaroid" />
							</div>
							<div class="business-ul" style="overflow:hidden;">
								<ul class="unstyled the-icons">
									<li><i class="icon-map-marker"></i> <span class="street"><?php echo $model->address; ?></span></li>
									<li><i class="icon-map-marker white"></i> <span class="city"><?php echo $model->postcode . ' ' . $model->city; ?></span></li>
								</ul>
								<ul class="unstyled the-icons">
									<li><i class="icon-phone"></i> <span class="phone"><?php echo $model->phone; ?></span></li>
								</ul>
								<?php echo $hours_list; ?>
								<ul class="unstyled the-icons">
									<li><i class="icon-external-link"></i> <a href="<?php echo $model->url_site; ?>" target="_blank"><?php echo $model->url_site; ?></a></li>
									<li><i class="icon-facebook-sign"></i> <a href="<?php echo $model->url_facebook_page; ?>" target="_blank"><?php echo $model->url_facebook_page; ?></a></li>
								</ul>
							</div>
						</div>
						<hr />
						<div class="business-brief">
							<?php echo strip_tags($model->brief); ?>
						</div>
					</div>
					</div>
				</div>
			</div>
		</div>
		<div class="span5">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter blue"><?php echo BUtils::T('detail_features_lbl'); ?></h3>
					<div class="widget-toolbar">
						<a href="<?php echo Yii::app()->createUrl('/tools/merchant/feature', array('id'=>$model->id)); ?>">
							<i class="icon-edit"></i> Edit labels
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
					<div class="widget-main">
<?php
echo MerchantObject::HtmlDetailFeatures($tag);
?>
					</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<div class="space-6"></div>
	
	<div class="row-fluid">
		<div class="span7">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter blue"><?php echo BUtils::T('gallery_lbl'); ?></h3>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main">
<?php $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
	'id' => 'uploadFile',
	'postParams' => array('prefix' => 'gal_'),
	'config' => array(
		'buttonName' => 'Upload new photo',
		'action' => Yii::app()->createUrl('/tools/merchant/uploadGallery', array('id'=>$model->id)),
		'allowedExtensions'=>array("jpg","jpeg","gif","png"),
		'sizeLimit'=>8*1024*1024,
		'onComplete' => "js:function(id, fileName, responseJSON){ handleUploaded(responseJSON); }",
	),
)); ?>	
							<hr />
<?php
if (count($gallery_rows) > 0) {
	echo '<div class="gallery">';
	foreach ($gallery_rows as $r) {
		$url_thumb = BUtils::GetOfferThumbUrl($r['url_picture']);
		$url_picture = BUtils::GetOfferPictureUrl(str_replace('thumb_', '', $r['url_picture']));
		$comments = Yii::app()->locale->dateFormatter->formatDateTime($r['ts_create_time']);
		$image_close = Yii::app()->baseUrl . '/img/btn-close.png';
		echo <<<HTML
<div class="photo relative" id="{$r['id']}">
	<div class="actions">
		<a href="#dlg-delete" role="button" class="remove-icon" data-toggle="modal" data-id="{$r['id']}" data-url="{$r['url_picture']}"><img src="{$image_close}" /></a>
	</div>
	<div class="thumb-wrap">
		<a href="{$url_picture}" data-rel="colorbox"><img src="{$url_thumb}" alt="" /></a>
	</div>
	<div class="comments">
		<span>{$comments}</span>
	</div>
	<div class="clearfix"></div>
</div>
HTML;
	}
	echo '</div>';
	echo '<div class="clear"></div>';
}
?>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="span5">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter red">Merchant comments</h3>
					<div class="widget-toolbar">
						<a href="#dlg-rate" role="button" data-toggle="modal">
							<i class="icon-edit"></i> Edit comments
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main" id="admin-comments">
<?php foreach ($rate_rows as $r) {
	$comments = strip_tags($r['comments']);
echo <<<HTML
	<div id="dlg-rate-{$r['id']}">
		<div>{$r['admin_name']} : {$r['interest_rate']}</div>
		<div>{$comments}</div>
	</div>
HTML;
} ?>
						</div>
					</div>
				</div>
			</div>
		</div>
		
	</div>
</div>

<!-- Modal -->
<div id="dlg-delete" class="modal hide" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3>Delete the photo</h3>
	</div>
	<div class="modal-body">
		<p>Are you sure you want to delete this photo?</p>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
		<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true" id="dlg-delete-confirm">Confirm</button>
	</div>
</div>

<!-- Modal -->
<div id="dlg-rate" class="modal hide" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3>Give your comments to this merchant?</h3>
	</div>
<?php $form = $this->beginWidget('CActiveForm', array(
	'id'=>'dlg-rate-form',
	//'enableAjaxValidation'=>true,
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>
	<div class="modal-body">
		<div class="control-group row-fluid">
			<?php echo $form->labelEx($model_rate,'interest_rate'); ?>
			<div class="controls">
				<?php echo $form->dropDownList($model_rate,'interest_rate',array(1=>1,2,3,4,5), array('class'=>'','empty'=>'')); ?>
				<?php echo $form->error($model_rate,'interest_rate'); ?>
			</div>
			<?php echo $form->labelEx($model_rate,'comments'); ?>
			<div class="controls">
				<?php echo $form->textArea($model_rate,'comments', array('class'=>'span12')); ?>
				<?php echo $form->error($model_rate,'comments'); ?>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
		<button class="btn btn-primary" id="dlg-rate-confirm" data-mid="<?php echo $model->id; ?>">Save</button>
	</div>
<?php $this->endWidget(); ?>
</div>

<script type="text/javascript">
$(function() {
	var colorbox_params = {
		reposition:true,
		scalePhotos:true,
		scrolling:false,
		previous:'<i class="icon-arrow-left"></i>',
		next:'<i class="icon-arrow-right"></i>',
		current:'{current} of {total}',
		maxWidth:'100%',
		maxHeight:'100%',
		onOpen:function(){
			document.body.style.overflow = 'hidden';
		},
		onClosed:function(){
			document.body.style.overflow = 'auto';
		},
		onComplete:function(){
			$.colorbox.resize();
		}
	};
	$('.photo [data-rel="colorbox"]').colorbox(colorbox_params);
	
	//show delete button
	$('.photo').mouseenter(function() {
		$(this).addClass('selected').children('.actions').show();
	}).mouseleave(function(){
		$(this).removeClass('selected').children('.actions').hide();
    });
	
	//delete button action
	var delete_id = null;
	var delete_url = null;
	$('.remove-icon').click(function(){
		delete_id = $(this).data('id');
		delete_url = $(this).data('url');
	});
	$('#dlg-delete-confirm').click(function() {
		if (delete_id) {
			$('#'+delete_id).remove();
			$.get('<?php echo Yii::app()->createUrl('/tools/merchant/deleteGallery'); ?>', {id:delete_id, url:delete_url, mid:<?php echo $model->id; ?>});
		}
	});
	
	//rate button
	$('#dlg-rate-confirm').click(function(ev){
		ev.preventDefault();
		var _button = $(this);
		$.ajax({dataType:'json', type:'POST', cache:false,
			url:'<?php echo Yii::app()->createUrl('/tools/merchant/saveRate',array('mid'=>$model->id, 'id'=>$model_rate->id)); ?>',
			data:$(this).parent().parent('form').serialize(),
			beforeSend:function(){ _button.attr('disabled', true); },
			error:function(){ _button.attr('disabled', false); },
			success:function(data){
				_button.attr('disabled', false);
				if (data.success) {
					$('#dlg-rate-'+data.id).remove();
					$('#admin-comments').prepend(data.html);
					$('#dlg-rate').modal('hide');
				} else {
					$.each(data, function(i, val) {
						$('#SysMerchantRateForm_'+i+'_em_').html(val[0]).show();
					});
				}
			}
		});
	});
});
function handleUploaded(respJSON) {
	if (respJSON.error) {
		$('.qq-upload-button').html('<span>'+respJSON.error+'</span>');
	} else {
		window.location.reload();
	}
}
</script>